﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;

namespace SharpMap.Presentation.Wpf
{
    public class StyleViewEntry : INotifyPropertyChanged
    {
        string _name = null;
        object _value = null;
        string _category ;
        PropertyDescriptor _propertyDescriptor = null;
        object _sourceInstanse = null;
        public string Category
        {
            get { return _category; }
            set { _category = value; RaisePropertyChanged("Category"); }
        }

        public StyleViewEntry(object source, PropertyDescriptor propDesc)
            : this(null, default(object), "General",source,propDesc) 
        { 
        }
        
        public StyleViewEntry(string name,object source, PropertyDescriptor propDesc)
            : this(name, default(object), "General", source, propDesc)
        {
            
        }

        public StyleViewEntry(string name, object val,object source, PropertyDescriptor propDesc)
            :this(name,val, "General",source,propDesc)
        {
        }

        public StyleViewEntry(string name, object val, string cat,
            object source, PropertyDescriptor propDesc)
        {
            //IMPORTANT the following assignemts should always be first
            // in the ctor
            _sourceInstanse = source;
            _propertyDescriptor = propDesc;


            Name = name;
            Value = val;
            _category = cat;
        }

        void UndelyingPropertyChanged(object sender, EventArgs e)
        {
            
            object newVal = _propertyDescriptor.GetValue(_sourceInstanse);
            if (!object.Equals(Value,newVal))
             {
                Value = newVal;
            }
        }


        public object Value
        {            
            get { return _value; }
            set 
            {
                
                if (!object.Equals(_value,value))
                {
                    _value = value;
                    _propertyDescriptor.SetValue(_sourceInstanse, _value);
                    RaisePropertyChanged("Value");
                }
               
            }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; RaisePropertyChanged("Name"); }
        }

        public void UpdateStyle(object style)
        {
            object value = _value;

            if (!_propertyDescriptor.PropertyType.IsAssignableFrom(value.GetType()))
            {
                value = _propertyDescriptor.Converter.ConvertFrom(_value);
            }
            _propertyDescriptor.SetValue(style, value);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        
        void RaisePropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion
    }

    
}
